import { ProTableColumn } from '@/components/pro-table/type'
import { NButton, NPopconfirm } from 'naive-ui'
import { DictModel } from '@/api/model/dictModel'

type props = {
  handleEditDict: (row: DictModel) => void
  fetchDelDict: (id: number) => void
}

export const useDictProTable = ({ handleEditDict, fetchDelDict }: props) => {
  const router = useEncryptRouter()
  const columns: ProTableColumn<DictModel>[] = [
    {
      title: '字典名称',
      key: 'name',
      query: {
        type: 'input'
      }
    },
    {
      type: 'link',
      title: '字典标识',
      key: 'type',
      query: {
        type: 'input'
      },
      click(row) {
        router.push({
          name: 'DictDataList',
          query: {
            id: row.id,
            type: row.type
          }
        })
      }
    },
    {
      type: 'tag',
      title: '状态',
      key: 'validFlag',
      query: {
        type: 'select'
      },
      dictOptions: [
        {
          value: true,
          label: '正常',
          type: 'primary'
        },
        {
          value: false,
          label: '停用',
          type: 'warning'
        }
      ]
    },
    {
      title: '备注',
      key: 'remark'
    },
    {
      title: '创建时间',
      key: 'createTime'
    },
    {
      title: '操作',
      key: 'operation',
      render(row) {
        return (
          <>
            {useHasPermi('dictionaryType:edit') && (
              <NButton
                type="primary"
                size="small"
                onClick={() => handleEditDict(row)}
                quaternary
              >
                修改
              </NButton>
            )}
            {useHasPermi('dictionaryType:del') && (
              <NPopconfirm onPositiveClick={() => fetchDelDict(row.id)}>
                {{
                  trigger: () => (
                    <NButton size="small" type="error" quaternary>
                      删除
                    </NButton>
                  ),
                  default: () => '您确定要删除？'
                }}
              </NPopconfirm>
            )}
          </>
        )
      }
    }
  ]

  return {
    columns
  }
}
